﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZLDC.Common.BaseUser;
using ZLDC.Common.Estate;

namespace ZLDC.Estate.Page
{
    public partial class Index : _PageBase
    {
        protected string changered;
        public bool menu_bool = false;

        /// <summary>
        /// 查询是否弹出对话框
        /// </summary>
        protected void GetT()
        {
            string sql = "select count(1) from lp_Configuration_main where workDay <'" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "' and isConfirm=0 and deptID=" + this.info.m_sDeptID;
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (rtcount(ds))
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "0")
                {
                    hidstation.Value = this.info.m_sStation;
                }
            }
            // sql = "select  COUNT(1) from v_usr_link where t_id=" + this.info.m_sDeptID + " and status not in('离职','停薪留职') and dltflg=0 and station!='分行经理' and station!='公盘'  and id is not null";
            sql = "select COUNT(1) from lp_Configuration_main where Convert(varchar,workDay,23)='" + DateTime.Now.ToString("yyyy-MM-dd") + "' and deptID=" + this.info.m_sDeptID + " and Other1!='0' and Other1!='1'";
            ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (rtcount(ds))
            {

                if (ds.Tables[0].Rows[0][0].ToString() != "0")
                {
                    hidstation.Value = this.info.m_sStation;
                }
            }
        }

        public bool rtcount(DataSet ds)
        {
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 查询员工是否弹出界面
        /// </summary>
        protected void GetuserType()
        {
            string sql = "select Other1,isConfirm,leave from lp_Configuration_main where workDay ='" + DateTime.Now.ToString("yyyy-MM-dd") + "' and (Other1='1' or Other1='0' or isConfirm=0) and choiceType!=2 and userID=" + this.info.m_sUserID;
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                labOther1.Value = "fk";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbUserName.Text = this.info.m_sFgs + "/" + this.info.m_sDeptName + "/" + this.info.m_sUserName + "[" + this.info.m_sStation.Replace("业务主任", "置业顾问*") + "]";
                userid.Value = this.info.m_sUserID;

                #region 量化平台 
                string DeptArea = ConfigurationManager.AppSettings["DeptArea"].ToString();
               // menu_bool = ZLDC.Topo.BLL.Estate.lp_Basis.GetJurisdiction(DeptArea);
                menu_bool = true;
                if (menu_bool)
                {
                    iframe1.Attributes.Add("src", "Desk1.aspx");
                    if (ZLDC.Topo.BLL.Estate.lp_Basis.GetStationhz(this.info.m_sStation))
                    {
                        hidstation.Value = this.info.m_sStation;
                        GetuserType();
                    }
                    else
                    {
                        GetT();
                    }
                }
                else
                {
                    iframe1.Attributes.Add("src", "Desk.aspx");
                }
                #endregion

                //hidstation.Text = this.info.m_sStation;
                //lbUserName.Text = this.info.m_sFgs + "/" + this.info.m_sDeptName + "/" + this.info.m_sUserName + "[" + this.info.m_sStation.Replace("业务主任", "置业顾问*") + "]";
                userid.Value = this.info.m_sUserID;

                Literal lt = new Literal();
                //string html = GetMenu();

                // 根据用户角色ID进行菜单缓存
                string html = "";
                string cachekey = "menu_" + this.info.m_sRoleID;
                int cachetime = 60 * 20;        // 缓存20分钟
                if (!menu_bool)//量化平台试用。。。。。福田北
                {
                    if (_Cache.GetCache(cachekey) == null || _Cache.GetCache(cachekey).ToString() == "")
                    {
                        string json = GetMenu();
                        _Cache.SetCache(cachekey, json, cachetime);
                    }
                    html = _Cache.GetCache(cachekey).ToString();
                }
                else
                {
                    string cachekey_test = "menu_test_" + this.info.m_sRoleID;
                    if (_Cache.GetCache(cachekey_test) == null || _Cache.GetCache(cachekey_test).ToString() == "")
                    {
                        string json = GetMenu();
                        _Cache.SetCache(cachekey_test, json, cachetime);
                    }
                    html = _Cache.GetCache(cachekey_test).ToString();
                }

                lt.Text = html;
                this.Page.Header.Controls.Add(lt);

                // 流动信息
                changered = ZLDC.Topo.BLL.Estate.CallBoardBLL.GetNewTopInfo();
                if (changered == "")
                {
                    changered = "今日无数据";
                }

                // 保存用户授权的查看类型   保存用户类型是否商业部、住宅
                try
                {
                    ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(_Convert.ToInt(this.info.m_sDeptID, 0));
                    string usage = "";  // 如1,4 表示 住宅、商住的权限(1住宅 2商铺 3写字楼 4商住 5仓库 6厂房 7地皮 8项目盘 9其他* 10民房)

                    if (info != null && info.ID > 0)
                    {
                        if (info.Useage != "")
                        {
                            // 判断字符串最后是否带","
                            string lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                            if (lastChar.Contains(","))
                                usage = info.Useage.Substring(0, info.Useage.Length - 1);
                            else
                                usage = info.Useage;
                        }

                        _Session.SetSession("UsageTypeIDList", usage);
                    }

                    // 0表示登录者为住宅业务员 1表示登录者为商铺业务员
                    if (usage.Contains("1"))
                        _Session.SetSession("isUsage", "0");
                    else
                        _Session.SetSession("isUsage", "1");
                }
                catch { }


                // hTodayWork.Value = ZLDC.Common.Estate._Cookie.GetCookie("TodayWorks");

                //hTodayWork.Value = "";
            }
        }

        /// <summary>
        /// 获取菜单 json 数据
        /// </summary>
        /// <returns></returns>
        protected string GetMenu()
        {
            string strJS = "<script>";
            strJS += "var _menus = {\"menus\":[";
            strJS += GetNavMenu();
            strJS += "]};</script>";
            return strJS;
        }

        /// <summary>
        /// 菜单详细信息
        /// </summary>
        /// <returns></returns>
        protected string GetNavMenu()
        {
            DataTable dt = ZLDC.Topo.BLL.Estate.MenuBLL.GetT_s_pdmParentMenu("楼盘管理系统");
            string strHtml = string.Empty;
            strHtml = " ";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string icon = "icon-sys";
                string menu = dt.Rows[i]["menu"].ToString();
                switch (menu)
                {
                    case "常用工具":
                        icon = "icon-tm";
                        break;
                    case "客源信息管理":
                        icon = "icon-cm";
                        break;
                    case "楼盘字典":
                        icon = "icon-em";
                        break;
                    case "盘源信息管理":
                        icon = "icon-pm";
                        break;
                    case "统计报表":
                        icon = "icon-um";
                        break;
                    case "系统管理":
                        icon = "icon-sm";
                        break;

                }

                if (!menu_bool && menu == "量化管理工具")
                {

                }
                else
                {
                    strHtml += "{\"menuid\":\"" + (i + 1) + "\",\"icon\":\"" + icon + "\",\"menuname\":\"" + menu +
                               "\",";
                    strHtml += "	\"menus\":[";
                    strHtml += GetLeftMenu(dt.Rows[i]["menu"].ToString(), i);
                    strHtml += "]";
                    strHtml += "},";
                }

               
            }

            if (strHtml != "")
                return strHtml.Substring(0, strHtml.Length - 1);
            else
                return "";
        }

        /// <summary>
        /// 获取详细菜单
        /// </summary>
        /// <returns></returns>
        protected string GetLeftMenu(string strPdm, int count)
        {
            string type = strPdm;
            string strHtml = string.Empty;

            DataTable dt = ZLDC.Topo.BLL.Estate.MenuBLL.GetT_s_pdmByMdl(type);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (CheckMenu(dt.Rows[i]["pdm"].ToString()))
                {
                    string icon = dt.Rows[i]["icon"].ToString();
                    if (string.IsNullOrEmpty(icon))
                    {
                        icon = "icon-nav";
                    }
                    strHtml += "{\"menuid\":\"m" + i.ToString() + (count + 1) + "\",\"menuname\":\" " + dt.Rows[i]["pdm"].ToString() + "\",\"icon\":\"" + icon + "\",\"url\":\"" + dt.Rows[i]["url"].ToString() + "\"},";
                }
            }
            if (strHtml != "")
                return strHtml.Substring(0, strHtml.Length - 1);
            else
                return "";
        }

        /// <summary>
        /// 检查是否有该权限
        /// </summary>
        /// <param name="strMdl"></param>
        /// <returns></returns>
        protected bool CheckMenu(string pdm)
        {
            bool b = false;

            if (ZLDC.Common.BaseUser.UserInfo.GetPopedom(pdm))
            {
                b = true;
            }
            return b;
        }

    }

}
